package me.zhanshi123.vipstorage;

import com.google.gson.Gson;
import me.zhanshi123.VipSystem.VipAPI;
import me.zhanshi123.vipstorage.command.Commands;
import me.zhanshi123.vipstorage.util.Database;
import me.zhanshi123.vipstorage.util.Update;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.MessageFormat;

public final class Main extends JavaPlugin {
    private static ConfigManager configManager;

    private static Main instance;

    private Update update;

    public static Main getInstance() {
        return instance;
    }

    private static VipAPI vipAPI;

    public static VipAPI getVipAPI() {
        return vipAPI;
    }

    public static ConfigManager getConfigManager() {
        return configManager;
    }

    @Override
    public void onEnable() {
        instance = this;
        Plugin vipSystem = Bukkit.getPluginManager().getPlugin("VipSystem");
        configManager = new ConfigManager();
        MessageManager.init();
        if (vipSystem == null) {
            Bukkit.getConsoleSender().sendMessage(MessageManager.getString("VipSystemNotFound"));
            setEnabled(false);
            return;
        }
        if (Double.parseDouble(vipSystem.getDescription().getVersion()) < 3.0d) {
            Bukkit.getConsoleSender().sendMessage(MessageManager.getString("VipSystemNotSupported"));
            setEnabled(false);
            return;
        }
        vipAPI = VipAPI.getInstance();
        new Database(vipAPI.getConnection());
        new Commands("vips");
        Bukkit.getConsoleSender().sendMessage(MessageManager.getString("fetchUpdate"));
        new BukkitRunnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL("http://www.zhanshi123.me/update/index.php?name=VipStorage");
                    InputStream in = url.openStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
                    String json = br.readLine();
                    update = new Gson().fromJson(json, Update.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (update != null) {
                    if (instance.getDescription().getVersion().equalsIgnoreCase(String.valueOf(update.getVersion()))) {
                        Bukkit.getConsoleSender().sendMessage(MessageManager.getString("latestVersion"));
                    } else {
                        Bukkit.getConsoleSender().sendMessage(MessageFormat.format(MessageManager.getString("foundNewVersion"), update.getVersion()));
                    }
                } else {
                    new BukkitRunnable() {
                        @Override
                        public void run() {
                            Bukkit.getConsoleSender().sendMessage(MessageManager.getString("fetchUpdateFailed"));
                        }
                    }.runTask(instance);
                }
            }
        }.runTaskAsynchronously(instance);
        Bukkit.getConsoleSender().sendMessage(MessageManager.getString("loadComplete"));
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }
}
